"""
Case Type   : 资源池化集群支持多链路
Case Name   : 备节点加入过程中断开链路1
Create At   : 2025/04/13
Owner       : @kyleze
Description :
    1.踢出备节点
    2.备机加入，并切断开链路1
    3.获取备机加入结果
    4.恢复链路，查询集群状态
Expect      :
    1.成功
    2.成功
    3.备机加入成功
    4.集群正常
History     :
"""
import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro


class MultipathTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pri_user_node = Node('PrimaryDbUser')
        self.pri_root = Node(node="PrimaryRoot")
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.sta1_root = Node(node="Standby1Root")
        self.ip_1 = macro.STANDBY1_LUN_IP.split()[0].strip()
        self.log.info("----获取备节点网络设备名称----")
        self.st1_network_card = ''
        interfaces = self.sta1_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_1 in item:
                self.st1_network_card = item.split(' ')[1].split(':')[0]
        self.log.info(self.st1_network_card)

    def test_multipath(self):
        step = '--step1:踢出备节点 expect:成功--'
        self.log.info(step)
        stop_res = self.sta1_sh.exec_cm_ctl("stop", '-n 2')
        self.log.info(stop_res)
        self.assertIn(self.constant.cm_stop_node_success_msg, stop_res,
                      '执行失败' + step)

        step = '--step2:备机加入，并切断开链路1 expect:成功--'
        self.log.info(step)
        start_thread = \
            ComThread(self.sta1_sh.exec_cm_ctl,
                      args=('start', '-n 2',))
        start_thread.setDaemon(True)
        start_thread.start()
        time.sleep(2)
        res = self.sta1_root.sh(f'ifdown {self.st1_network_card}').result()
        self.log.info(res)
        self.assertIn("successfully deactivated", res, "执行失败" + step)
        start_thread.join(120)

        step = '--step3:获取备机加入结果 expect:成功--'
        self.log.info(step)
        start_res = start_thread.get_result()
        self.log.info(start_res)
        self.assertIn(self.constant.cm_start_node_success_msg, start_res,
                      "执行失败" + step)

        step = '--step4:恢复链路，查询集群状态 expect:集群正常--'
        self.log.info(step)
        res = self.sta1_root.sh(f'ifup {self.st1_network_card}').result()
        self.log.info(res)
        self.assertIn("successfully activated", res, "执行失败" + step)
        cluster_status = self.primary_sh.get_db_cluster_status('status')
        self.log.info(cluster_status)
        self.assertTrue(cluster_status, "执行失败" + step)

    def tearDown(self):
        self.log.info(f'----{os.path.basename(__file__)} end----')

